<?php
 
require 'cp_init.php';
$action = strtolower(trim($_REQUEST['action']));
if($action=='query')
{
$callerid = addslashes(trim($_REQUEST['callerid']));
$sn = addslashes(trim($_REQUEST['num']));
$query_id = addslashes(trim($_REQUEST['serialno']));
$now = get_gmtime();
$coupon_data = $GLOBALS['db']->getRow('select c.confirm_time,c.end_time,doi.name as name,c.sn as sn,doi.sub_name as sub_name,doi.unit_price as price from '.DB_PREFIX.'deal_coupon as c left join '.DB_PREFIX."deal_order_item as doi on c.order_deal_id = doi.id where c.sn = '".$sn."' and c.is_valid = 1 and c.is_delete = 0 and c.begin_time <".$now);
if($coupon_data)
{
if(($coupon_data['end_time']==0||$coupon_data['end_time']>$now)&&$coupon_data['confirm_time']==0)
{
$msg = $GLOBALS['lang']['COUPON_VERIFY_LOG'].':('.$callerid.')'.sprintf($GLOBALS['lang']['COUPON_IS_VALID'],$coupon_data['sub_name'],$coupon_data['sn']);
log_coupon($sn,$msg,$query_id);
header('Content-type: text/xml; charset=utf-8');
$xml="<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n";
$xml.="<coupon>\r\n";
$xml.="<result>3</result>\r\n";
$xml.='<id>'.$coupon_data['sn']."</id>\r\n";
$xml.='<product>'.$coupon_data['sub_name']."</product>\r\n";
$xml.='<price>'.round($coupon_data['price'],2)."</price>\r\n";
$xml.='</coupon>';
echo $xml;
exit;
}
else
{
if($coupon_data['confirm_time']>0)
{
$msg = $GLOBALS['lang']['COUPON_VERIFY_LOG'].':('.$callerid.')'.sprintf($GLOBALS['lang']['COUPON_INVALID_USED'],to_date($coupon_data['confirm_time']));;
log_coupon($sn,$msg,$query_id);
header('Content-type: text/xml; charset=utf-8');
$xml="<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n";
$xml.="<coupon>\r\n";
$xml.="<result>1</result>\r\n";
$xml.="<id></id>\r\n";
$xml.="<product></product>\r\n";
$xml.="<price></price>\r\n";
$xml.='</coupon>';
echo $xml;
exit;
}
if($coupon_data['end_time']!=0&&$coupon_data['end_time']<$now)
{
$msg = $GLOBALS['lang']['COUPON_VERIFY_LOG'].':('.$callerid.')'.$GLOBALS['lang']['COUPON_ENDED'];
log_coupon($sn,$msg,$query_id);
header('Content-type: text/xml; charset=utf-8');
$xml="<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n";
$xml.="<coupon>\r\n";
$xml.="<result>2</result>\r\n";
$xml.="<id></id>\r\n";
$xml.="<product></product>\r\n";
$xml.="<price></price>\r\n";
$xml.='</coupon>';
echo $xml;
exit;
}
}
}
else
{
$msg = $GLOBALS['lang']['COUPON_VERIFY_LOG'].':('.$callerid.')'.$GLOBALS['lang']['COUPON_INVALID'];
log_coupon($sn,$msg,$query_id);
header('Content-type: text/xml; charset=utf-8');
$xml="<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n";
$xml.="<coupon>\r\n";
$xml.="<result>0</result>\r\n";
$xml.="<id></id>\r\n";
$xml.="<product></product>\r\n";
$xml.="<price></price>\r\n";
$xml.='</coupon>';
echo $xml;
exit;
}
}
elseif($action=='consume')
{
$callerid = addslashes(trim($_REQUEST['callerid']));
$sn = addslashes(trim($_REQUEST['num']));
$query_id = addslashes(trim($_REQUEST['serialno']));
$pwd = addslashes(trim($_REQUEST['secret']));
$now = get_gmtime();
$coupon_data = $GLOBALS['db']->getRow('select c.id as id,doi.name as name,doi.sub_name as sub_name,doi.unit_price as price,c.sn as sn,c.supplier_id as supplier_id,c.confirm_time as confirm_time from '.DB_PREFIX.'deal_coupon as c left join '.DB_PREFIX."deal_order_item as doi on c.order_deal_id = doi.id where c.sn = '".$sn."' and c.password = '".$pwd."' and c.is_valid = 1 and c.is_delete = 0  and c.begin_time <".$now.' and (c.end_time = 0 or c.end_time>'.$now.')');
if($coupon_data)
{
if($coupon_data['confirm_time'] >0)
{
$msg = $GLOBALS['lang']['COUPON_USE_LOG'].':('.$callerid.')'.sprintf($GLOBALS['lang']['COUPON_INVALID_USED'],to_date($coupon_data['confirm_time']));
log_coupon($coupon_data['sn'],$msg,$query_id);
header('Content-type: text/xml; charset=utf-8');
$xml="<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n";
$xml.="<coupon>\r\n";
$xml.="<result>false</result>\r\n";
$xml.='</coupon>';
echo $xml;
exit;
}
else
{
$GLOBALS['db']->query('update '.DB_PREFIX.'deal_coupon set confirm_account = 999999,confirm_time='.$now.' where id = '.intval($coupon_data['id']));
$msg = $GLOBALS['lang']['COUPON_USE_LOG'].':('.$callerid.')'.sprintf($GLOBALS['lang']['COUPON_USED_OK'],to_date($now));
log_coupon($coupon_data['sn'],$msg,$query_id);
send_use_coupon_sms(intval($coupon_data['id']));
send_use_coupon_mail(intval($coupon_data['id']));
header('Content-type: text/xml; charset=utf-8');
$xml="<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n";
$xml.="<coupon>\r\n";
$xml.="<result>true</result>\r\n";
$xml.='<product>'.$coupon_data['sub_name']."</product>\r\n";
$xml.='<price>'.round($coupon_data['price'],2)."</price>\r\n";
$xml.='</coupon>';
echo $xml;
exit;
}
}
else
{
$msg = $GLOBALS['lang']['COUPON_USE_LOG'].':('.$callerid.')'.$GLOBALS['lang']['PASSWORD_ERROR'];
log_coupon($sn,$msg,$query_id);
header('Content-type: text/xml; charset=utf-8');
$xml="<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n";
$xml.="<coupon>\r\n";
$xml.="<result>false</result>\r\n";
$xml.='</coupon>';
echo $xml;
exit;
}
}
else
{
header('Content-Type:text/html; charset=utf-8');
echo '非法访问';
exit;
}

?>